package com.whc.gulimall.member.controller;

import com.whc.common.exception.BizCodeEnume;
import com.whc.common.utils.PageUtils;
import com.whc.common.utils.R;
import com.whc.common.utils.RRException;
import com.whc.gulimall.member.entity.MemberEntity;
import com.whc.gulimall.member.exception.PhoneExistsException;
import com.whc.gulimall.member.exception.UsernameExistsException;
import com.whc.gulimall.member.feign.CouponFeignService;
import com.whc.gulimall.member.service.MemberService;
import com.whc.gulimall.member.vo.LoginVo;
import com.whc.gulimall.member.vo.RegisterVo;
import com.whc.gulimall.member.vo.SocialUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author wanghaochen
 * @email angaoen@outlook.com
 * @date 2022-03-05 00:23:42
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Resource
    private CouponFeignService couponFeignService;

    @GetMapping("/coupons")
    public R listMemberCoupons() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("我去");
        R r = couponFeignService.listMemberCoupons();
        return R.ok().put("member", memberEntity).put("coupons", r.get("coupons"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 注册
     *
     * @param vo vo
     * @return 统一返回结果
     */
    @PostMapping("/regist")
    public R register(@RequestBody RegisterVo vo) {
        try {
            memberService.register(vo);
        } catch (PhoneExistsException e) {
            return R.error(BizCodeEnume.USER_EXISTS_EXCEPTION);
        } catch (UsernameExistsException e) {
            return R.error(BizCodeEnume.PHONE_EXISTS_EXCEPTION);
        }
        return R.ok();
    }

    /**
     * 登录
     *
     * @param vo vo
     * @return 统一返回结果
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginVo vo) {
        try {
            memberService.login(vo);
        } catch (Exception e) {
            return R.error(BizCodeEnume.LOGIN_EXCEPTION);
        }
        return R.ok();
    }

    /**
     * oauth2登录
     *
     * @param socialUserVo 社交用户登录
     * @return 统一返回结果
     */
    @PostMapping("/oauth2/login")
    public R oathTwoLogin(@RequestBody SocialUserVo socialUserVo) {
        try {
            return R.ok().put("data", memberService.oauthTwoLogin(socialUserVo));
        } catch (RRException e) {
            return R.error(BizCodeEnume.OAUTH2_NET_EXCEPTION);
        }

    }

}
